/*
 * CTemplateCollection.cpp
 *
 *  Created on: 17.12.2011
 *      Author: Kirill Blinov
 */

#include "CTemplateCollection.h"
#include "CStaticUnit.h"
#include "CDynamicUnit.h"


CTemplateCollection::CTemplateCollection() : newCurrentUnit(NULL)
{

}

CTemplateCollection::~CTemplateCollection()
{
	Clear();
}

IUnit*
CTemplateCollection::CreateUnitInstance( const UNIT_PHYSIC_PARAMS &PhysicParams, const std::string TemplateName,
		                                 const std::string UnitName )
{
	//Clear newCurrentUnit
	newCurrentUnit = NULL;
	std::map<std::string,IUnit*>::iterator iter = templates.find( TemplateName );

	// if a template exists
	if ( iter != templates.end() )
	{
		IUnit* Template = iter->second;
		Template->MakeClone( this );
		newCurrentUnit->SetPhysicParams( PhysicParams );
		newCurrentUnit->SetName( UnitName );
	}

	//newCurrentUnit was filled in by CreateClone() called by Template
	return newCurrentUnit;
}

bool
CTemplateCollection::AddTemplate( IUnit* Template, const std::string TemplateName )
{
	std::map<std::string,IUnit*>::const_iterator iter = templates.find( TemplateName );

	bool Result = ( templates.end() == iter );

	if ( Result )
	{
		templates.insert( std::pair<std::string,IUnit*>(TemplateName, Template) );
	}

	return Result;
}

void
CTemplateCollection::Clear()
{
	templates.clear();
}

void
CTemplateCollection::CreateClone( const CHeavyJet& Pattern )
{
	CHeavyJet *NewUnit = new  CHeavyJet( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CMediumJet& Pattern )
{
	CMediumJet *NewUnit = new  CMediumJet( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CLightJet& Pattern )
{
	CLightJet *NewUnit = new  CLightJet( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CRacket& Pattern )
{
	CRacket *NewUnit = new  CRacket( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CMilk& Pattern )
{
	CMilk *NewUnit = new  CMilk( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CBullet& Pattern )
{
	CBullet *NewUnit = new  CBullet( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CBonus& Pattern )
{
	CBonus *NewUnit = new  CBonus( Pattern );
	newCurrentUnit = NewUnit;
}

void
CTemplateCollection::CreateClone( const CScenary& Pattern )
{
	CScenary *NewUnit = new  CScenary( Pattern );
	newCurrentUnit = NewUnit;
}
